/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.lib;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 *
 * @author tiepvv
 */
public class ZDayStat {

    private int TSECOND = 60 * 60 * 24;
    private AtomicLong[] _data;

    public ZDayStat() {
        _data = new AtomicLong[TSECOND];
        for (int i = 0; i < TSECOND; i++) {
            _data[i] = new AtomicLong();
        }
    }

    public long get(int pos) {
        if (pos < 0 || pos > TSECOND) {
            return -1;
        }
        return _data[pos].get();
    }

    long set(int pos, long value) {
        if (pos < 0 || pos > TSECOND) {
            return -1;
        }
        _data[pos].set(value);
        return value;
    }

    long inc(int pos) {
        if (pos < 0 || pos > TSECOND) {
            return -1;
        }
        return _data[pos].incrementAndGet();
    }

    long dec(int pos) {
        if (pos < 0 || pos > TSECOND) {
            return -1;
        }
        return _data[pos].decrementAndGet();
    }

    long add(int pos, long value) {
        if (pos < 0 || pos > TSECOND) {
            return -1;
        }
        return _data[pos].addAndGet(value);
    }

    long sub(int pos, long value) {
        if (pos < 0 || pos > TSECOND) {
            return -1;
        }
        return _data[pos].addAndGet(-1 * value);
    }

    void clear() {
        for (int i = 0; i < TSECOND; i++) {
            _data[i].set(0);
        }
    }

    long maxDataFromDate(int from, int to) {
        /* validate */
        if (from < 0 || from > TSECOND) {
            return -2;
        }
        if (to < 0 || to > TSECOND) {
            return -2;
        }
        long current_mv = 0;
        for (int i = from; i <= to; i++) {
            if (_data[i].get() > current_mv) {
                current_mv = _data[i].get();
            }
        }
        return current_mv;
    }

    long minDataFromDate(int from, int to) {
        /* validate */
        if (from < 0 || from > TSECOND) {
            return -2;
        }
        if (to < 0 || to > TSECOND) {
            return -2;
        }

        long current_mv = Long.MAX_VALUE;
        for (int i = from; i <= to; i++) {
            if (_data[i].get() < current_mv) {
                current_mv = _data[i].get();
            }
        }

        return current_mv;
    }

    float avgDataFromDate(int from, int to) {
        /* validate */
        if (from < 0 || from > TSECOND) {
            return -2;
        }
        if (to < 0 || to > TSECOND) {
            return -2;
        }
        if (from == to) {
            return -2;
        }
        long total = 0;
        long count = 0;
        for (int i = from; i <= to; i++) {
            if (_data[i].get() > 0) {
                total += _data[i].get();
                count++;
            }
        }
        return (count > 0) ? ((float) total) / (to - from) : 0;
    }

    int getDataFromDate(int from, int to, List<Long> rpsData) {
        if (to - from == 0) {
            return -1;
        }
        if (from < 0 || from > TSECOND) {
            return -2;
        }
        if (to < 0 || to > TSECOND) {
            return -2;
        }
        if (to - from > 3600) {
            return -3;
        }
        for (int i = from; i <= to; i++) {
            rpsData.add(_data[i].get());
        }
        return (to - from);
    }

    static int getCurrentSecond() {
        Date current = new Date();
        return current.getHours() * 60 * 60 + current.getMinutes() * 60 + current.getSeconds();
    }
}
